/**
 * Created with IntelliJ IDEA
 * Description:
 * User: SEKI TOMOKI
 * Date: 2023-09-16
 * Time: 21:45
 */
import java.util.Arrays;

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int[] shortArray = nums1.length > nums2.length ? nums2 : nums1;
        int[] longArray = nums1.length > nums2.length ? nums1 : nums2;

        int[] tmp = new int[shortArray.length];
        int indexOfIntersection = 0;

        for(int i = 0; i < longArray.length; i++) {
            if(i >= 1 && longArray[i] == longArray[i - 1]) {
                continue;
            }
            for(int j = 0; j < shortArray.length; j++) {
                if(longArray[i] == shortArray[j]) {
                    tmp[indexOfIntersection] = longArray[i];
                    indexOfIntersection++;
                    break;
                }
            }
        }

        int[] intersection = new int[indexOfIntersection];
        for(int i = 0; i < indexOfIntersection; i++) {
            intersection[i] = tmp [i];
        }
        return intersection;
    }
}
